// 格式化日期为 yyyy-MM-dd
// 传入的参数为 Date 对象或者时间戳
function formatDate(date) {
    const d = new Date(date);
    const year = d.getFullYear();
    const month = ('0' + (d.getMonth() + 1)).slice(-2);
    const day = ('0' + d.getDate()).slice(-2);
    return `${year}-${month}-${day}`;
}

//获取15天的开始时间结束时间
export function getPast15Days() {
    const today = new Date();
    const startDate = new Date(today.getTime() - 15 * 24 * 60 * 60 * 1000);
    return {
        startDate: formatDate(startDate),
        endDate: formatDate(today),
    };
}

//获取过去一个月的开始时间结束时间
export function getPastMonth() {
    const today = new Date();
    const startDate = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());
    return {
        startDate: formatDate(startDate),
        endDate: formatDate(today),
    };
}

//获取上个月的开始时间结束时间
export function getLastMonth() {
    const today = new Date();
    const lastMonthEndDate = new Date(today.getFullYear(), today.getMonth(), 0);
    const lastMonthStartDate = new Date(today.getFullYear(), today.getMonth() - 1, 1);

    return {
        startDate: formatDate(lastMonthStartDate),
        endDate: formatDate(lastMonthEndDate),
    };
}


//根据传入的时间戳 返回时间
export function formatTimestamp(timestamp, format = 'YYYY-MM-DD hh:mm:ss') {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = ('0' + (date.getMonth() + 1)).slice(-2);
    const day = ('0' + date.getDate()).slice(-2);
    const hours = ('0' + date.getHours()).slice(-2);
    const minutes = ('0' + date.getMinutes()).slice(-2);
    const seconds = ('0' + date.getSeconds()).slice(-2);

    let formattedDate = format
        .replace('YYYY', year)
        .replace('MM', month)
        .replace('DD', day)
        .replace('hh', hours)
        .replace('mm', minutes)
        .replace('ss', seconds);

    return formattedDate;
}


//计算两天之间的天数差
export function daysBetween(timestampInSeconds) {
    const timestampInMilliseconds = timestampInSeconds * 1000;
    const currentDate = new Date();
    const inputDate = new Date(timestampInMilliseconds);

    // 将日期设置为当天的开始时间（凌晨 0 点 0 分 0 秒）
    currentDate.setHours(0, 0, 0, 0);
    inputDate.setHours(0, 0, 0, 0);

    const millisecondsPerDay = 24 * 60 * 60 * 1000;
    const daysDifference = Math.abs(currentDate - inputDate) / millisecondsPerDay;

    return daysDifference;
}

//根据传入的秒 转为分钟 或者秒
export function formatDuration(seconds) {
    if(seconds<0){
        return '暂无'
    }
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = seconds % 60;

    let formattedDuration = '';

    if (minutes > 0) {
        formattedDuration += `${minutes}分钟`;
    }

    if (remainingSeconds > 0) {
        formattedDuration += `${remainingSeconds}秒`;
    }

    return formattedDuration || '暂无';
}


//获取今日日期


export function getToday() {
    const today = new Date();
    const year = today.getFullYear();
    const month = ('0' + (today.getMonth() + 1)).slice(-2);
    const day = ('0' + today.getDate()).slice(-2);

    return `${year}-${month}-${day}`;
}


export function getCurrentQuarter() {
    const today = new Date();
    const currentMonth = today.getMonth();
    const quarterStartMonth = Math.floor(currentMonth / 3) * 3;
    const quarterEndMonth = quarterStartMonth + 2;

    const quarterStartDate = new Date(today.getFullYear(), quarterStartMonth, 1);
    const quarterEndDate = new Date(today.getFullYear(), quarterEndMonth + 1, 0);

    return {
        startDate: formatDate(quarterStartDate),
        endDate: formatDate(quarterEndDate),
    };
}

export function getCurrentYear() {
    const today = new Date();
    const yearStartDate = new Date(today.getFullYear(), 0, 1);
    const yearEndDate = new Date(today.getFullYear(), 11, 31);

    return {
        startDate: formatDate(yearStartDate),
        endDate: formatDate(yearEndDate),
    };
}

export const toTimeStringZh = (time) => {
    if(/,/.test(time)) {
        time = Number(time.replace(/,/g, ''))
    }
    // const d = Math.floor(time / (24 * 3600))
    // time = time - d * 24 * 3600
    const h = Math.floor(time / 3600)
    time = time - h * 3600
    const m = Math.floor(time / 60)
    time = time - m * 60
    const s = time
    // ${d > 0 ? d + "天" : ""}
    return `${h > 0 ? h + "小时" : ""}${m > 0 ? m + "分" : ""}${s > 0 ? s + "秒" : ""}`
}